<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { GetBrandofficialFenrunList } from '@/api/modules/brandofficial'
import GetCacheData from '@/store/modules/NewPageCache'

const route = useRoute()
const router = useRouter()
const cacheData = GetCacheData()
let PageNum = cacheData.GetCacheData('brandofficialFenrunList').currentPage

const findFrom = reactive({
  OrderNo: route.query.OrderNo ? route.query.OrderNo : '',
  ...(cacheData.GetCacheData('brandofficialFenrunList').fromData),
})
const dataList: any = reactive({
  list: [],
  option: [],
})
const loading = ref(true)
const total = ref(0)

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.OrderNo && ({ OrderNo: findFrom.OrderNo })),
    ...(findFrom.OutOrderNo && ({ OutOrderNo: findFrom.OutOrderNo })),
    ...(findFrom.WxOrderId && ({ WxOrderId: findFrom.WxOrderId })),
    ...(findFrom.UserId && ({ UserId: findFrom.UserId })),
    ...(findFrom.UserMobile && ({ UserMobile: findFrom.UserMobile })),
    ...(findFrom.SCOpenId && ({ SCOpenId: findFrom.SCOpenId })),
    ...(findFrom.Flag !== '~' && ({ Flag: findFrom.Flag })),
    ...(findFrom.FenRunTime.length !== 0 && ({ FenRunStartTime: findFrom.FenRunTime[0], FenRunEndTime: findFrom.FenRunTime[1] })),
  }
  await GetBrandofficialFenrunList(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      total.value = dataRes.data.total
      loading.value = false
    }
    else {
      ElMessage.error(`获取数据失败。${dataRes.error}`)
    }
  })
  loading.value = false
}
// 查询
function Inquire() {
  GetDataList(1, cacheData.GetCacheData('brandofficialFenrunList').currentSize)
  cacheData.SetCacheData('brandofficialFenrunList', PageNum, cacheData.GetCacheData('brandofficialFenrunList').currentSize, findFrom)
}

// 跳转详情
function GetDetail(id: number) {
  router.push({
    name: 'brandofficialFenrunDetail',
    query: {
      id,
    },
  })
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  cacheData.SetCacheData('brandofficialFenrunList', PageNum, val)
}
function handleCurrentChange(val: number) {
  GetDataList(val, cacheData.GetCacheData('brandofficialFenrunList').currentSize)
  cacheData.SetCacheData('brandofficialFenrunList', val, cacheData.GetCacheData('brandofficialFenrunList').currentSize)
  PageNum = val
}

const nowPageNum = computed(() => {
  return cacheData.GetCacheData('brandofficialFenrunList').currentPage || 1
})

onMounted(() => {
  GetDataList(cacheData.GetCacheData('brandofficialFenrunList').currentPage, cacheData.GetCacheData('brandofficialFenrunList').currentSize)
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="订单编号">
                <el-input v-model="findFrom.OrderNo" placeholder="请输入订单编号" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="商户分账单号">
                <el-input v-model="findFrom.OutOrderNo" placeholder="请输入商户分账单号" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="微信分账单号">
                <el-input v-model="findFrom.WxOrderId" placeholder="请输入微信分账单号" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="用户ID">
                <el-input v-model="findFrom.UserId" placeholder="请输入用户ID" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="授权手机号">
                <el-input v-model="findFrom.UserMobile" placeholder="请输入授权手机号" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="分账人OpenId">
                <el-input v-model="findFrom.SCOpenId" placeholder="请输入分账人OpenId" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="状态">
                <el-select v-model="findFrom.Flag" placeholder="请选择状态">
                  <el-option label="全部" value="~" />
                  <el-option label="发放成功" :value="1" />
                  <el-option label="发放失败" :value="2" />
                </el-select>
              </el-form-item>
              <el-form-item label="下单时间">
                <el-date-picker
                  v-model="findFrom.FenRunTime" type="datetimerange" clearable value-format="YYYY-MM-DD HH:mm:ss"
                  range-separator="到" start-placeholder="开始日期" end-placeholder="结束日期"
                />
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="id" label="账户ID" width="100" />
        <el-table-column align="center" prop="orderNo" label="订单ID" width="260" />
        <el-table-column align="center" prop="subscriber.userId" label="顾客ID" width="100" />
        <el-table-column align="center" prop="subscriber" label="顾客信息" width="200">
          <template #default="scope">
            <div v-if="!scope.row.subscriber">
              未授权
            </div>
            <div v-else class="user-name">
              <el-avatar :size="40" style="margin-right: 10px;" :src="scope.row.subscriber?.userAvatar" />
              <div>
                <p style="margin: 0;">
                  {{ scope.row.subscriber?.userNickName }}
                </p>
                <span>
                  {{ scope.row.subscriber?.userPhone }}
                </span>
              </div>
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" prop="openId" label="分账人OpenID" width="220" /> -->
        <el-table-column align="center" prop="outOrderNo" label="商户分账单号" width="280" />
        <el-table-column align="center" prop="fenrunAmount" label="分润金额" width="150" />
        <el-table-column align="center" prop="fenrunTime" label="分润时间" width="220" />
        <el-table-column header-align="center" prop="wxDesp" label="分账描述" width="220" />
        <el-table-column align="center" prop="flag" label="状态" width="150">
          <template #default="scope">
            <el-tag>
              {{ scope.row.flag === 1 ? '发放成功'
                : scope.row.flag === 2 ? '发放失败'
                  : scope.row.flag === 0 ? '待发放' : '未定义' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="wxErrCode" label="错误代码" width="220">
          <template #default="scope">
            <span v-if="scope.row.flag === 2">
              {{ scope.row.wxErrCode }}
            </span>
            <span v-else>
              无
            </span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="wxErrCodeDes" label="错误代码描述" width="220">
          <template #default="scope">
            <span v-if="scope.row.flag === 2">
              {{ scope.row.wxErrCodeDes }}
            </span>
            <span v-else>
              无
            </span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="payTime" label="支付时间" width="220" />
        <el-table-column header-align="center" prop="desp" label="描述" width="220" />
        <el-table-column align="center" prop="addTime" label="添加时间" width="220" />
        <el-table-column align="center" prop="updateTime" label="更新时间" width="220" />
        <el-table-column align="center" label="操作" fixed="right" min-width="140">
          <template #default="scope">
            <el-button type="primary" @click="GetDetail(scope.row.id)">
              查看明细
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="cacheData.GetCacheData('brandofficialFenrunList').currentSize"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        :default-current-page="nowPageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}

.user-name {
  display: flex;
  align-items: center;
  justify-content: left;

  div {
    text-align: left;
  }
}
</style>
